package org.json.custom;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * JsonDeserializer: 用于将指定 JSON 字符串解析回 Java 对象的自定义反序列化器（Deserializer）。
 *
 * @author yexi
 */
public class CustomJsonDeserializer extends JsonDeserializer<Date> {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String valueAsString = p.getValueAsString();
        try {
            return DATE_FORMAT.parse(valueAsString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
